define(['jquery', 'bootstrap', 'backend', 'table', 'form',], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hotels/index',
                    add_url: 'hotels/add',
                    edit_url: 'hotels/edit',
                    del_url: 'hotels/del',
                    multi_url: 'hotels/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'category',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk:'hid',
                sortName:'hid',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'hid', title: __('Id'),operate: false},
                        {field: 'h_img', title: __('Image'), operate: false, formatter: Table.api.formatter.image},//height:'50px'
                        {field: 'h_title', title: __('h_title'), align: 'left'},
                        {field: 'h_storey', title: __('h_storey'), align: 'left'},
                        {field: 'h_dw', title: __('h_unit'), align: 'left'},
                        {field: 'h_tel', title: __('h_tel'), align: 'left'},
                        {field: 'h_phone', title: __('h_phone'), align: 'left'},
                        {field: 'h_address', title: __('h_address'), align: 'left'},
                        {field: 'h_status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]
                ],
                commonSearch: false,

            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //修改添加是弹窗大小
            $(".btn-add").data("area", ["80%","80%"]); //自定义弹窗大小
            //修改编辑弹窗大小
            table.on('post-body.bs.table',function () {
                $(".btn-editone").data("area", ["80%","80%"]); //自定义弹窗大小
            });

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },

        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        edit: function () {

            //选中房间设施
            var h_servsval = $("#h_servs").val();

            if(h_servsval != ""){
                var h_servsvalarr = new Array(); //定义一数组
                h_servsvalarr = h_servsval.split(",");
                for (i=0;i<h_servsvalarr.length ;i++ )
                {
                    $("#h_servsval input[value="+ h_servsvalarr[i] +"]").attr("checked","checked");

                }
            }

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            },

        }
    };
    return Controller;
});
